import utils from '../../../utils/utils'
import Dialog from '../../../dist/dialog/dialog';
import { taskhallHttp } from '../../../api/task'
import Data from "../../../utils/data";
import User from "../../../utils/user";

const taskModel = new taskhallHttp();

Page({

  /**
   * 页面的初始数据
   */
  pushOld:false,
  isTabBar: true,
  data: {
    s_ekey:"",
    initUser:true,
    userInfo:{},
    userInit:false,
    isShowMainPopup:false,
    mainPopupInfo:{},
    dict:{},
    activeTask: 0, // 当前在做的任务
    balanceNum: 0, // 余额
    totalAwardMoney: 0, // 累计发出奖励
    taskAwardList: [], // 任务奖励列表
    banner: [], // banner滚动
    redpacketBg: utils.pageUrl + "mini/images/redpacket/icon_nav_bg.png", // 红包背景
    openLogin: false,
  },

  initPage (options) {
    let self = this;
    let app  = getApp();
    console.log('initPage');
    this.initData();
    app.addEvent('upUserInfo', function (userInfo) {
      userInfo['tuan'] = {};
      if (JSON.stringify(userInfo) !== JSON.stringify(self.data.userInfo)) {
        utils.log('upUserInfo');
        if (!userInfo.uid) {
          self.setData({
            userInfo: {
              common: {
                coupon_count: 0,
                card_blance: 0,
                pea_blance: 0,
              },
            },
            initUser: false
          });
        } else {
          self.setData({
            userInfo: userInfo,
            initUser: true
          });
          self.initData();
        }
      }
    });
    if(options.hasOwnProperty('ekey')){
      utils.UserUv(options.ekey);
      User.UvTrackPut(options.ekey);
    }
  },

  initData(){
    let self = this;
    Data.getDictConfig('task_config', function (res) {
      self.setData({
        dict: res
      })
    }, true);
    self.getUserInfo(function (res) {
      self.setData({
        userInit: true,
        initUser: true,
        userInfo: res,
        balanceNum: res.common.card_blance,
      });
      self.httpMainPopup()
    }, function () {
      self.setData({
        userInit: false
      });
    }, true);
    this.httpRedpacketTaskInfo();
  },
  httpMainPopup(){
    taskModel.mainPopup({}).then(res=>{
      this.setData({
        isShowMainPopup: res.is_popup,
        mainPopupInfo: res.popup
      })
    })
  },

  // 任务奖励
  onTaskAward (event) {
    const { status ,id} = event.currentTarget.dataset;
    if(this.data.userInit === false){
      this.setData({
        initUser:false
      });
      return true;
    }
    if (status === 1) {
      Dialog.confirm({
        title: '温馨提示',
        message: '请依次完成之前的任务，才能解锁当前任务',
        confirmButtonText: '前往任务',
        cancelButtonText: '我知道了'
      }).then(() => {
        wx.navigateTo({
          url:"/pages/task/taskDetails/index?id=" + this.data.activeTask
        })
      }).catch(() => {
        // on cancel
      })
    } else {
      wx.navigateTo({
        url:"/pages/task/taskDetails/index?id=" + id
      })
    }
  },

  // 提现
  onWithdraw () {
    if(this.data.userInit === false){
      this.setData({
        initUser:false
      });
      return true;
    }
    wx.navigateTo({
      url: "/pages/me/balance/index"
    })
  },

  // 客服
  onService () {
    wx.navigateTo({
      url: '/pages/me/service/service'
    })
  },

  // 获取红包任务信息
  httpRedpacketTaskInfo () {
    taskModel.getRedpacketTaskInfo().then(res => {
      wx.stopPullDownRefresh();
      let activeTask = 0;
      for(let i = 0;i<res.red_package_task.length;i++){
        if(res.red_package_task[i]['status'] === 2){
          activeTask = res.red_package_task[i]['id'];
        }
      }
      this.setData({
        totalAwardMoney: res.cumulative_reward,
        taskAwardList: res.red_package_task,
        banner: res.banner,
        activeTask: activeTask,
      })
    }).catch((err) =>{
      console.log(err);
    })
  },
  onPullDownRefresh() {
    this.initData();
  },
  onShowMainPopup(){
    this.setData({
      isShowMainPopup: !this.data.isShowMainPopup
    })
  }
});
